package sg.edu.nus.iss.vmcs.controller;

import sg.edu.nus.iss.vmcs.entity.AbstractStoreFactory;
import sg.edu.nus.iss.vmcs.entity.Coin;
import sg.edu.nus.iss.vmcs.entity.CashStore1;
import sg.edu.nus.iss.vmcs.entity.StoreItem;
import sg.edu.nus.iss.vmcs.util.Environment;


public class CashStoreController extends StoreController1 {
	protected static CashStoreController cashStoreController = null;
	
	protected void init(String modelName){
		setModelName(modelName);
		setStore(AbstractStoreFactory.getInstance(Environment.getMachineModel()).getCashStoreInstance());
	}
	
	private CashStoreController(String modelName){
		init(modelName);
	}
	
	public static CashStoreController getInstance(){
		if(cashStoreController == null ){
			cashStoreController = new CashStoreController(Environment.getMachineModel());
		}
		return cashStoreController;
	}
	
	public Coin findCoin(double weight) {
		Coin coin = null;
		StoreItem[] items = cStore.getItems();
		for(int i =0;i<items.length;i++){
			if(items[i] == null || items[i].getContent() == null)
				continue;
			if(((Coin)items[i].getContent()).getWeight() == weight)
				coin = (Coin)items[i].getContent();
		}
		return coin;
	}
}
